/*
 * MainForm.java
 *
 * Created on 26  2007, 05:46
 * Note: This source code is protected under the This software is licensed under the CC-GNU GPL.
 */
package OpenToday.GUI;

import OpenToday.Configuration.OpenTodayConfiguration;
import OpenToday.IManager;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Spacer;
import javax.microedition.lcdui.StringItem;

/**
 *
 * @author Menny Even Danan (c) 2007. Note: This source code is protected under the This software is licensed under the CC-GNU GPL.
 */
public class MainForm extends Form implements IOpenTodayGUI {

    private static Spacer CreateSpacer() {
        return new Spacer(1000, 3);
    }

    private static Font getDayFont() {
        return Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, OpenTodayConfiguration.getInstance().getTitleFontSize());
    }

    private static Font getEventFont() {
        return Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, OpenTodayConfiguration.getInstance().getEventFontSize());
    }
    private int m_firstLineOffset;
    private final IManager m_manager;

    /** Creates a new instance of MainForm */
    public MainForm(IManager manager) {
        super("Open Today");
        super.setTitle(null);
        m_manager = manager;
        m_firstLineOffset = 40;
    }

    public void RepaintRequired() {
        //nothing to do
    }

    public void Clear() {
        super.deleteAll();
        Spacer fromTop = new Spacer(1000, m_firstLineOffset);
        super.append(fromTop);
    }

    public void SetFirstLineOffset(int offset) {
        m_firstLineOffset = offset;
    }

    public void AddDayLine(String dayString) {
        StringItem todayString = new StringItem(null, dayString);
        todayString.setFont(getDayFont());
        todayString.setLayout(StringItem.LAYOUT_LEFT);
        super.append(todayString);
        //spacer for the next appoitment
        super.append(CreateSpacer());
    }

    public void AddLine(String text) {
        StringItem line = new StringItem(null, text);
        line.setFont(getEventFont());
        line.setLayout(StringItem.LAYOUT_LEFT);
        super.append(line);
        //spacer for the next appoitment
        super.append(CreateSpacer());
    }

    public void AddErrorLine(String errorString) {
        AddLine(errorString);
    }

    public void AddEventLine(boolean currentEvent, String eventTime, String eventString) {
        String fullEventString = currentEvent ? ">" : "*";
        fullEventString += eventTime + " - " + eventString;
        //ToDo correct length calculation, also for all day events
        // substring because of line breake
        int maxLength = 22;
        if (fullEventString.length() < 22) {
            maxLength = fullEventString.length();
        }

        StringItem line = new StringItem(null, fullEventString.substring(0, maxLength));
        line.setFont(getEventFont());
        line.setLayout(StringItem.LAYOUT_LEFT);
        super.append(line);
        //spacer for the next appoitment
        super.append(CreateSpacer());
    }

    public void AddEventLine(boolean currentEvent, String startTime, String endTime, String eventString) {
        String fullEventString = currentEvent ? ">" : "*";
        fullEventString += startTime + " - " + endTime + " - " + eventString + "\n";
        StringItem line = new StringItem(null, fullEventString);
        line.setFont(getEventFont());
        line.setLayout(StringItem.LAYOUT_LEFT);
        super.append(line);
        //spacer for the next appoitment
        super.append(CreateSpacer());
    }

    public void AddAllDayEventLine(String eventString) {
        StringItem line = new StringItem(null, eventString);
        line.setFont(getEventFont());
        line.setLayout(StringItem.LAYOUT_LEFT);
        super.append(line);
        //spacer for the next appoitment
        super.append(CreateSpacer());
    }

    public void AddToDoLine(boolean overdue, boolean completed, String todoString) {
        AddAllDayEventLine(todoString);
    }

    public Displayable getDisplayable() {
        return this;
    }

    public void SetDayColor(int R, int G, int B) {
    }

    public void SetEventColor(int R, int G, int B) {
    }

    public void SetBackgroundColor(int R, int G, int B) {
    }

    public void SetBackgroundImage(String fullImageName) {
    }
}
